package com.online_exam.controller.admin.education;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.online_exam.base.RestResponse;
import com.online_exam.domain.Subject;
import com.online_exam.viewmodel.admin.education.SubjectEditVm;
import com.online_exam.service.SubjectService;
import com.online_exam.viewmodel.admin.education.SubjectPage;
import com.online_exam.viewmodel.admin.education.SubjectPageRequestVM;
import com.online_exam.viewmodel.admin.education.SubjectResponseVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/admin/education/subject")
public class SubjectController {

    private SubjectService subjectService;
    private ModelMapper modelMapper;
    @Autowired
    public SubjectController(SubjectService subjectService,ModelMapper modelMapper){
        this.subjectService=subjectService;
        this.modelMapper=modelMapper;
    }


    /**
     * //添加科目 ； 视图类->数据库实体类
     * @param subjectEditVm 接收到的视图类
     * @return
     */
    @PostMapping("/edit")
    public RestResponse editSubject(@RequestBody @Valid SubjectEditVm subjectEditVm){
        if (null == subjectEditVm.getId()) {
            Integer id = subjectEditVm.getId();
            String name = subjectEditVm.getName();
            Integer level = subjectEditVm.getLevel();
            String levelName = subjectEditVm.getLevelName();
            Subject subject = new Subject();
            subject.setId(id);
            subject.setName(name);
            subject.setLevel(level);
            subject.setLevelName(levelName);
            subject.setItemOrder(1);
            subject.setDeleted(false);
            int index = subjectService.addSubject(subject);
            if (index > 0) {
                System.out.println("添加成功...");
            } else {
                System.out.println("添加失败...");
            }
        }else{

            subjectService.updateSubject(subjectEditVm);
        }
        return RestResponse.ok();
    }

    //查询学科
    @RequestMapping("/select/{id}")
    public RestResponse<SubjectEditVm> findById(@PathVariable("id") Integer id){
        Subject subject = subjectService.findById(id);
        SubjectEditVm subjectEditVm= new SubjectEditVm();
        subjectEditVm.setId(subject.getId());
        subjectEditVm.setLevel(subject.getLevel());
        subjectEditVm.setLevelName(subject.getLevelName());
        subjectEditVm.setName(subject.getName());
        return  RestResponse.ok(subjectEditVm);
    }
    @RequestMapping("/list")
    public RestResponse<List<Subject>> findAll(){
        List<Subject> serviceAll = subjectService.findAll();
        return RestResponse.ok(serviceAll);
    }

    //
//    @RequestMapping(value = "/subject/page", method = RequestMethod.POST)
//    public RestResponse<PageInfo<SubjectResponseVM>> pageList(@RequestBody SubjectPageRequestVM model) {
//        PageInfo<Subject> pageInfo = subjectService.page(model);
//        PageInfo<SubjectResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> modelMapper.map(e, SubjectResponseVM.class));
//        return RestResponse.ok(page);
//    }
    @PostMapping("/page")
    public RestResponse<SubjectPage> page(@RequestBody SubjectPageRequestVM model){
        Page<Object> page = PageHelper.startPage(model.getPageIndex(), model.getPageSize());
        List<Subject> subjects = subjectService.page(model);
        List<SubjectResponseVM> responseVMS = subjects.stream().map(subject -> {
            SubjectResponseVM responseVM = modelMapper.map(subject, SubjectResponseVM.class);
            return responseVM;
        }).collect(Collectors.toList());
        SubjectPage subjectPage = new SubjectPage();
        subjectPage.setPageIndex(page.getPageNum());
        subjectPage.setPageIndex(page.getPageSize());
        subjectPage.setList(responseVMS);
        return RestResponse.ok(subjectPage);
    }
    @PostMapping("/delete/{id}")
    public RestResponse deleteById(@PathVariable Integer id){
        subjectService.deleteById(id);
        return RestResponse.ok();
    }

}
